package com.four.marketing.service.impl;

import com.alibaba.fastjson.JSON;
import com.four.marketing.entity.Collage;
import com.four.marketing.dao.CollageDao;
import com.four.marketing.feign.ShopFeginService;
import com.four.marketing.service.CollageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.marketing.vo.CollageVo;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 侯新伟
 * @since 2021-12-14
 */
@Service
public class CollageServiceImpl extends ServiceImpl<CollageDao, Collage> implements CollageService {

    @Autowired
    private ShopFeginService shopFeginService;

    @Autowired
    private CollageDao collageDao;

    @Autowired
    private RestHighLevelClient client;
    /**
     * 添加拼团商品
     * @param collage
     */
    @Override
    public void addCollage(Collage collage) {
        collageDao.addCollage(collage);
    }

    /**
     * 查询所有商品
     * fegin调用
     */
    @Override
    public void findAllManage() {
        shopFeginService.findAllManage();
    }

    /**
     * 根据id查询商品信息
     * fegin调用
     * @param id
     */
    @Override
    public void getManageById(Integer id) {
        shopFeginService.getShopById(id);
    }

    /**
     * 查询所有的拼团商品
     * @param vo
     * @return
     */
    @Override
    public List<Collage> collageList(CollageVo vo) {

        return collageDao.collageList(vo);
    }

    /**
     * 将数据上传es
     * @param id
     */
    @Override
    public void upCollage(Integer id) {
        Collage collage=collageDao.selectCollageById(id);
        IndexRequest request = new IndexRequest("collage");
        request.id(collage.getCoId().toString());
        String jsonString = JSON.toJSONString(collage);
        request.source(jsonString, XContentType.JSON);
        try {
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
            //修改数据库中此数据的状态
            int sts=1;
            collageDao.updateCollageStatus(id,sts);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *根据id从es中删除数据
     * @param id
     */
    @Override
    public void downCollage(Integer id) {
        //修改数据库中此数据的状态
        int sts=0;
        collageDao.updateCollageStatus(sts,id);
        DeleteRequest request = new DeleteRequest("collage", id.toString());
        try {
            DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void selectCollageById(Integer id) {
        collageDao.selectCollageById(id);
    }

    @Override
    public void updateCollage(Integer id,Collage collage) {
        collage.setCoId(id);
        collageDao.updateCollage(collage);
    }

    @Override
    public void getPictureById(Integer id) {
        shopFeginService.getPictureById(id);
    }
}
